/*
 * PlayerRecoveredEvent.java
 * Team qq 2011
 */
package com.google.code.timetrail.backend;

import java.util.List;
import java.util.Random;

/**
 * This is the random event where the player recovers
 * 
 * @author Utkarsh
 * @version 1.1
 */
public class PlayerRecoveredEvent extends Event {
	/**
	 * PlayerRecoveredEvent's SerialVersion ID, to be changed when the object is
	 * changed
	 **/
	private static final long serialVersionUID = 5040L;
	
	/**
	 * the instance of the game that is acted upon by the event
	 */
	private final Control gameControl;
	
	/**
	 * The randomly selected person to be healed
	 */
	private final Person randomSickPerson;

	/**
	 * Randomly selects a player to heal. The sucess of this is random, and
	 * dependent upon whether or not the person is sick
	 * 
	 * @param gameControl
	 *            the instance of the game being acted upon by the event
	 */
	public PlayerRecoveredEvent(Control gameControl) {
		this.gameControl = gameControl;
		final Random rand = new Random();
		final List<Person> players = this.gameControl.getMembers();
		if (players.size() != 1) {
			final int removedPersonIndex = rand.nextInt(players.size() - 1) + 1;
			randomSickPerson = players.get(removedPersonIndex);
			if (!randomSickPerson.isDead() && randomSickPerson.isSick()) {
				super.formatMessage(randomSickPerson
						+ " has gotten recovered in the time stream.");
			} else {
				super.messageText = getDummyText();
			}
		} else {
			super.messageText = getDummyText();
			randomSickPerson = null;
		}
	}

	/**
	 * The action for this event
	 */
	@Override
	public void action() {
		randomSickPerson.heal();
	}
}
